package inabit.inabitcompiler.compiler;

import inabit.compiler.compiler.LanguageCompiler;
import inabit.data.InabitMethod;
import inabit.data.Rule;
import inabit.data.Variable;
import inabit.inabitcompiler.lexical.InabitLexicalAnalyzer;
import inabit.inabitcompiler.parser.InabitParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**@author Rafael Rocha*/
public class InabitCompiler extends LanguageCompiler<InabitLexicalAnalyzer, InabitParser>{

	private List<Variable> compiledVariables;
	private List<Rule> compiledRules;
	private Map<String, InabitMethod> methodMap;
	
	public InabitCompiler() {
		super(new InabitLexicalAnalyzer(), new InabitParser());
		compiledVariables = new ArrayList<Variable>();
		compiledRules = new ArrayList<Rule>();
		methodMap = new HashMap<String, InabitMethod>();
	}

	@Override
	protected void init(InabitLexicalAnalyzer lexer, InabitParser parser) {
		compiledVariables.clear();
		compiledRules.clear();
		parser.setMethods(methodMap);
	}

	@Override
	protected void deinit(InabitLexicalAnalyzer lexer, InabitParser parser) {
		this.compiledVariables.addAll(parser.getVariables());
		this.compiledRules.addAll(parser.getRules());
	}
	public List<Variable> getCompiledVariables(){
		return compiledVariables;
	}

	public void addMethod(InabitMethod method) {
		methodMap.put(method.getName(), method);
	}
	public void removeMethod(String methodName) {
		methodMap.remove(methodName);
	}
	public List<Rule> getCompiledRules() {
		return compiledRules;
	}

}
